package com.warrior.gatherweb.config;

import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Value("${swagger.controller}")
    private String controller;
    @Value("${swagger.title}")
    private String title;
    @Value("${swagger.service-url}")
    private String serviceUrl;
    @Value("${info.description}")
    private String des;
    @Value("${info.version}")
    private String ver;

    @Value("${swagger.gateway}")
    private String gateway;


    @Bean
    public Docket getDocket() {
        Docket result = new Docket(DocumentationType.SWAGGER_2);
        // 根据配置的网关地址修改swagger的主机地址
        if (!Strings.isNullOrEmpty(gateway)) {
            result = result.host(gateway);
        }
        result = result.apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(controller))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
        return result;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(des)
                .version(ver)
                .termsOfServiceUrl(serviceUrl)
                .build();
    }

    /**
     * 设置要验证的信息
     *
     * @return
     */
    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "Authorization", "header")
        );
    }

    /**
     * @return
     */
    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext
                        .builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
    }

    /**
     * 配置验证范围
     *
     * @return
     */
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(new SecurityReference("Authorization", authorizationScopes));
    }
}
